package com.shengruan.admin.action;

import java.util.List;

import com.shengruan.admin.jdbc.AdminJdbc;
import com.shengruan.admin.vo.Admin;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;
import com.shengruan.common.Md5;
import com.shengruan.framework.pager.Pager;
import com.shengruan.servlet.ServletUtils;

public class AdminManageAction extends BaseAction {

    private AdminJdbc adminJdbc = (AdminJdbc)this.getJdbc(AdminJdbc.class);

    public void add() throws Exception {
        String userName = this.input("userName");
        String password = this.input("password");
        String mobile = this.input("mobile");
        String email = this.input("email");

        Admin admin = new Admin();
        admin.setUsername(userName);
        admin.setPassword(new Md5().getMD5ofStr(password));
        admin.setMobile(mobile);
        admin.setEmail(email);
        adminJdbc.save(admin);

        this.setRequest(Constants.MESSAGE, "添加管理员成功。");
        this.forward("/admin/system/adminAdd.jsp");
    }

    public void checkUserName() throws Exception{
        String userName = this.input("userName");
        Admin admin = adminJdbc.getByAdminName(userName);
        if (null == admin) {
            this.print("true");
        } else {
            this.print("false");
        }
    }



    public void changePassword() throws Exception{
        String passwordOld = this.input("passwordOld");
        String password = this.input("password");
        Admin adminObj = (Admin) ServletUtils.getSessionAttribute(request, Constants.ADMIN);
        //密码与原始密码不同
        if (!adminObj.getPassword().equals(new Md5().getMD5ofStr(passwordOld))) {
            this.setRequest(Constants.MESSAGE, "旧密码不正确请重新输入。");
            this.forward("/admin/system/admin-change-password.jsp");
            return;
        }
        adminJdbc.changePassword(adminObj.getId().toString(), new Md5().getMD5ofStr(password));
        Admin admin = adminJdbc.getByAdminName(adminObj.getUsername());
        this.setSession(Constants.ADMIN, admin);
        this.setRequest(Constants.MESSAGE, "修改密码成功。");
        this.forward("/admin/system/admin-change-password.jsp");
    }


    public void list() throws Exception {
        String userName = this.input("userName");
        int currentPage = 1;
		try {currentPage = Integer.parseInt(this.input("currentPage")); } catch(Exception ex) { }

		Pager pager = new Pager(currentPage,adminJdbc.getRows(),30);
		List<Admin> userList = adminJdbc.findWithPage(currentPage, 30);

        this.setRequest("userName", userName);
		this.setRequest("pager", pager);
		this.setRequest("adminList", userList);


		this.forward("/admin/system/adminList.jsp");
    }


    public void delete() throws Exception {
        String id = this.input("id");
        adminJdbc.deleteById(id);

        this.setRequest(Constants.MESSAGE, "删除管理员成功。");


       this.redirect("/admin/system/admin-list.ac");
    }


    public void load() throws Exception {
        String id = this.input("id");
        Admin admin = adminJdbc.getById(id);
        this.setRequest(Constants.ADMIN, admin);
        this.forward("/admin/system/adminEdit.jsp");
    }


    public void edit() throws Exception {
        String id = this.input("id");
        String mobile = this.input("mobile");
        String email = this.input("email");

        adminJdbc.updateMobileAndEmail(id, mobile, email);

        Admin admin = adminJdbc.getById(id);
        this.setRequest(Constants.ADMIN, admin);
        this.setRequest(Constants.MESSAGE, "修改管理员信息成功。");
        this.forward("/admin/system/adminEdit.jsp");
    }


    public void changeStatus() throws Exception {
        String id = this.input("id");
        Admin admin = adminJdbc.getById(id);
        if (admin.getStatus() == 0) {
            admin.setStatus(1);
            this.setRequest(Constants.MESSAGE, "启用管理员成功。");
        } else {
            admin.setStatus(0);
            this.setRequest(Constants.MESSAGE, "禁用管理员成功。");
        }
        adminJdbc.updateStatus(id, admin.getStatus().toString());

        this.redirect("/admin/system/admin-list.ac");
    }
}
